package ui;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.ImageIcon;
import util.Pair;

public class ImageEditorImagePanel extends ImagePanel {
	private static final long serialVersionUID = 1L;
	
	private Point click = null;
	protected Point click2 = null;
	private boolean zoomnow = false;

	public void setZoomNow(boolean b) {
		this.zoomnow = b;
	}

	public ImageEditorImagePanel(String file) {		
		this(new ImageIcon(file).getImage());
	}

	public ImageEditorImagePanel(Image image) {
		super(image);		
	}

	public Pair<Point, Point> getCoords() {
		return new Pair<Point, Point>(new Point(click.x, click.y), new Point(
				click2.x, click2.y));
	}

	public void addCoordsListener() {
		this.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent ev) {
				if (zoomnow) {
					int x = (int) (ev.getPoint().getX());
					int y = (int) (ev.getPoint().getY());					
					ImageEditorImagePanel.this.click2 = null;
					ImageEditorImagePanel.this.click = new Point(x, y);
					repaint();
				}
			}

			@Override
			public void mouseReleased(MouseEvent ev) {
				if (zoomnow) {
					int x = (int) (ev.getPoint().getX());
					int y = (int) (ev.getPoint().getY());					
					ImageEditorImagePanel.this.click2 = new Point(x, y);
					repaint();
				}
			}
		});
		this.addMouseMotionListener(new MouseAdapter() {

			@Override
			public void mouseMoved(MouseEvent e) {
				if (zoomnow) {
					setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
				}
			}
		});
	}	

	@Override
	public void paintComponent(Graphics g) {
		g.drawImage(getImage(), 0, 0, null);
		if (click != null) {
			Graphics2D g2 = (Graphics2D) g.create();
			g2.setColor(Color.RED);
			g2.setStroke(new BasicStroke(3f));
			g2.drawLine(click.x - 5, click.y - 5, click.x + 5, click.y + 5);
			g2.drawLine(click.x - 5, click.y + 5, click.x + 5, click.y - 5);
			g2.setColor(Color.YELLOW);
			g2.setStroke(new BasicStroke(1f));
			g2.drawLine(click.x - 4, click.y - 4, click.x + 4, click.y + 4);
			g2.drawLine(click.x - 4, click.y + 4, click.x + 4, click.y - 4);
		}
		if (click2 != null) {
			Graphics2D g2 = (Graphics2D) g.create();

			g2.setColor(Color.RED);
			g2.setStroke(new BasicStroke(3f));
			g2.drawLine(click2.x - 5, click2.y - 5, click2.x + 5, click2.y + 5); // KREUZ
			g2.drawLine(click2.x - 5, click2.y + 5, click2.x + 5, click2.y - 5); // KREUZ
			g2.drawLine(click.x, click.y, click2.x, click.y); // KASTEN
			g2.drawLine(click.x, click.y, click.x, click2.y); // KASTEN
			g2.drawLine(click2.x, click2.y, click.x, click2.y); // KASTEN
			g2.drawLine(click2.x, click2.y, click2.x, click.y); // KASTEN

			g2.setColor(Color.YELLOW);
			g2.setStroke(new BasicStroke(1f));
			g2.drawLine(click2.x - 4, click2.y - 4, click2.x + 4, click2.y + 4); // KREUZ
			g2.drawLine(click2.x - 4, click2.y + 4, click2.x + 4, click2.y - 4); // KREUZ
			g2.drawLine(click.x, click.y, click2.x, click.y); // KASTEN
			g2.drawLine(click.x, click.y, click.x, click2.y); // KASTEN
			g2.drawLine(click2.x, click2.y, click.x, click2.y); // KASTEN
			g2.drawLine(click2.x, click2.y, click2.x, click.y); // KASTEN

		}
	}
}
